<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.UUID" %>
<%@ page import="myschool.entity.Student" %>
<%@ page import="myschool.service.StudentService" %>
<%@ page import="myschool.util.StrUtil" %><%--
  Created by IntelliJ IDEA.
  User: oldliu
  Date: 2023-06-09
  Time: 14:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <%
//      request.setCharacterEncoding("utf-8");
////      String name=request.getParameter("sname");
////      String idCardPic=request.getParameter("idCardPic");
////      out.print(name+","+idCardPic);
//      ServletInputStream in = request.getInputStream();
//      int ch=0;
//      while ((ch=in.read())!=-1){
//          out.print((char)ch);
//      }
      Student student=new Student();
      //暂存目录，java.io.tempdir
      DiskFileItemFactory df=new DiskFileItemFactory();
      ServletFileUpload upload=new ServletFileUpload(df);
      //中文问题
      upload.setHeaderEncoding("utf-8");
      List<FileItem> fileItems = upload.parseRequest(request);
      //文件域和表单域单独处理
      for(FileItem fi:fileItems){
          if(fi.isFormField()){//表单域
              out.print("<p>表单域：");
              out.print(fi.getFieldName()+"，值是："+fi.getString("utf-8"));
             if (fi.getFieldName().equals("sname")){
                 student.setSname(fi.getString("utf-8"));
             }else if (fi.getFieldName().equals("sex")){
                 student.setSex(fi.getString("utf-8"));
             }else if (fi.getFieldName().equals("telephone")){
                 student.setTelephone(fi.getString("utf-8"));
             }else if (fi.getFieldName().equals("qq")){
                 student.setQQ(fi.getString("utf-8"));
             }else if (fi.getFieldName().equals("weChat")){
                 student.setWechat(fi.getString("utf-8"));
             }else if (fi.getFieldName().equals("gradeID")){
                 student.setGradeid(Long.parseLong(fi.getString("utf-8")));
             }else if (fi.getFieldName().equals("enterdate")){
                 student.setEnterdate(StrUtil.toDate(fi.getString("utf-8")));
             }else if (fi.getFieldName().equals("birthday")){
                 student.setBirthday(StrUtil.toDate(fi.getString("utf-8")));
             }else if (fi.getFieldName().equals("city")){
                 student.setFromcity(fi.getString("utf-8"));
             }else if (fi.getFieldName().equals("address")){
                 student.setAddress(fi.getString("utf-8"));
             }else if (fi.getFieldName().equals("pwd")){
                 student.setPassword(fi.getString("utf-8"));
             }
              out.print("</p>");
              continue;
          }
          if(fi.getSize()<1){
              continue;
          }
          out.print("<p>文件域：");
          //需要换个名字，保留后缀
          out.print("文件名："+fi.getName());
          String oldName=fi.getName();
          String houzhui=oldName.substring(oldName.lastIndexOf('.'));
          String newName= UUID.randomUUID()+houzhui;
          String path0=application.getRealPath("img");
          out.print(path0+"<br/>");
          File path=new File(path0);
          if(!path.exists())
              path.mkdirs();
          fi.write(new File(path,newName));
          student.setIdcardpic("img/"+newName);
          out.print("<img src='img/"+newName+"' height='200px' width='300px'/>");
          out.print("</p>");
      }

      int r = StudentService.ME.add(student);
      if(r>0){
          out.print("增加成功");
      }else{
          out.print("增加失败！");
      }
  %>
</body>
</html>
